package com.example.lcode.list;

import javax.websocket.PongMessage;
import java.util.List;

public class Question61 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode rotateRight(ListNode head, int k) {
        if (head == null) {
            return head;
        }
        ListNode point = head;
        int number = 1;
        ListNode lastHead = null;
        while (point != null) {
            ListNode next = point.next;
            if (next == null) {
                point.next = head;
                lastHead = point;
                break;
            }
            number ++;
            point = point.next;
        }
        for (int i = 0; i < number - k % number; i++) {
            lastHead = head;
            head = head.next;
        }
        lastHead.next = null;
        return head;
    }

    public static void main(String[] args) {
        Question61.ListNode listNode1 = new Question61.ListNode(1);
        Question61.ListNode listNode2 = new Question61.ListNode(2);
        Question61.ListNode listNode3 = new Question61.ListNode(3);
        Question61.ListNode listNode4 = new Question61.ListNode(4);
        Question61.ListNode listNode5 = new Question61.ListNode(5);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        Question61 question24 = new Question61();
        question24.rotateRight(listNode1, 2);
    }

}
